<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2016/10/8
 * Time: 9:50
 */

namespace app\manage\controller;


class WeixinFllow extends Base
{
    private $mod;
    protected $beforeActionList = [
      'beforeEdit' => ['only'=>'add,edit']
    ];
    public function _initialize(){
        parent::_initialize();
        $this->_name = 'weixin_msg_replay';
        $this->mod = model('weixin_msg_replay');
        $this->list_field = 'id,keyword,type,create_time,update_time,status';
    }
    protected function _search(){
        $map['event_type']   = 3;//事件回复
        $this->assign('e_type',3);
        return $map;
    }
    protected function beforeEdit(){
        $this->assign('event_menu',$this->getEventMenu());
    }
    public function add(){
        $e_type = input('param.e_type/d',2);//关注回复
        if($e_type == 2 && $info = $this->hasFllowReplay()){
            $this->redirect('edit',['id'=>$info['id'],'type'=>$info['type']]);
        }

        $type = input('param.type/d',1);
        $tpl  = 'add_'.$type;
        $this->assign('type',$type);
        $this->assign('e_type',$e_type);
        $this->assign('model',$this->mod->getModel());//模型列表
        return $this->fetch($tpl);
    }
    public function addDo(){
        //后置事件 添加标签
        \app\common\model\WeixinMsgReplay::event('before_insert',function($obj){
            //查询 菜单是否已绑定回复
            if($obj->event_type == 3){
                $map['keyword'] = $obj->keyword;
                $map['event_type'] = 3;
                $count = $this->mod->where($map)->count('id');
                if($count > 0){
                    $this->error = '该菜单已绑定事件，若须修改请到列表中编辑！';
                    return false;
                }
            }

            return true;
        });
        parent::addDo();
    }
    public function edit(){
        $type = input('param.type/d',1);
        $id   = input('param.id/d');
        if($id){
            $info = $this->mod->where(['id'=>$id])->field('id,aid,model,event_type,keyword,data,type')->find();
            $tpl  = 'edit_'.$type;
            $info['keyword'] = model('weixin_msg_menu')->getMenuName($info['keyword']);
            $this->assign('info',$info);
            $this->assign('type',$type);
            $this->assign('model',$this->mod->getModel());//模型列表
            return $this->fetch($tpl);
        }else{
            $this->error('参数错误');
        }

    }
    public function editDo(){
        \app\common\model\WeixinMsgReplay::event('after_update',function($obj){
            if(isset($obj->keyword)){
                $tag = explode(',',$obj->keyword);
                if($tag){
                    $data = [];
                    foreach($tag as $v){
                        $data[] = [
                            'aid'  => $obj->id,
                            'name' => trim($v)
                        ];
                    }
                    if($data){
                        model('tags')->where(['aid'=>$obj->id])->delete();
                        model('tags')->saveAll($data,$data);
                    }
                }
            }
            return true;
        });
        parent::editDo();
    }
    public function ajaxGetList(){
        $pagesize = 10;
        $page = input('param.page/d',1);
        $keyword = input('param.keyword');
        $model   = input('param.model','article');
        $mod     = model($model);
        $field   = 'id,img';
        $map['status'] = 1;
        if($model == 'house'){
            $field .= ',concat(title," ",average_price,"元/㎡") as title,special_youhui as description';
        }else{
            $field .= ',title,description';
        }
        if($keyword){
            $map['title'] = ['like','%'.$keyword.'%'];
        }
        $count = $mod->where($map)->count('id');
        $lists =  $mod->field($field)->where($map)->page($page,$pagesize)->select();
        $return['total'] = ceil($count/$pagesize);
        $return['lists'] = $lists;
        if($lists){
            $this->ajaxReturn(1,'',$return);
        }else{
            $this->ajaxReturn(0,'无相关数据');
        }
    }
    /**
     * @return bool
     * 查询是否有关注回复内容
     */
    private function hasFllowReplay(){
        $map['event_type'] = 2;
        $info = $this->mod->where($map)->field('id,type')->find();
        if($info){
            return $info;
        }else{
            return false;
        }
    }

    /**
     * @return false|\PDOStatement|string|\think\Collection
     * 获取事件菜单 用于绑定回复操作
     */
    private function getEventMenu(){
        $mod = model('weixin_msg_menu');
        $map['type'] = 'click';
        $field = 'name,alias';
        $lists = $mod->where($map)->field($field)->select();
        return $lists;
    }
}